/*
 * @(#)MaintCalendarShareAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.calendar.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.calendar.bean.MtmCalendarUserUserActor;
import com.dcivision.calendar.dao.MtmCalendarUserUserActorDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.bean.UserRole;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRecordDAObject;
import com.dcivision.user.dao.UserRoleDAObject;

/**
  MaintCalendarShareAction.java

  This class is for maint calendar share.

    @author          Tony Chen
    @company         DCIVision Limited
    @creation date   06/12/2003
    @version         $Revision: 1.5 $
*/

public class MaintCalendarShareAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.5 $";

  public MaintCalendarShareAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.calendar.dao.MtmCalendarRecordSharingDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.SCHEDULE_CALENDAR);
  }

  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
      throws ServletException {
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }

    MaintMtmCalendarUserUserActorForm mtmCalendar2UserActorFrm = (MaintMtmCalendarUserUserActorForm)form;
    SessionContainer sessionCon = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);

    //get user full name list
    UserRecordDAObject userRecordDAO = new UserRecordDAObject(sessionCon,conn);
    List allUserList = userRecordDAO.getAllOtherUserFullNameByStatus(GlobalConstant.STATUS_ACTIVE,sessionCon.getUserRecordID());
    request.setAttribute("allUserList",allUserList);

    UserGroupDAObject userGroupDAO = new UserGroupDAObject(sessionCon,conn);
    UserRoleDAObject userRoleDAO = new UserRoleDAObject(sessionCon, conn);

    //get user full name by IDs
    String[] userIDs = mtmCalendar2UserActorFrm.getShareToUserIDs();
    if (!Utility.isEmpty(userIDs)) {
      List userFullNameList = new ArrayList();
      for (int i = 0; i < userIDs.length; i++) {
        UserRecord userRecord = (UserRecord) (userRecordDAO.getObjectByID(
            TextUtility.parseIntegerObj(userIDs[i])));
        userFullNameList.add(userRecord.getFullName());

      }
      request.setAttribute("userFullNameList",userFullNameList);
    }

    //get group name by IDs
    String[] groupIDs = mtmCalendar2UserActorFrm.getShareToUserGroups();
    if (!Utility.isEmpty(groupIDs)) {
      List groupNameList = new ArrayList();
      for (int i = 0; i < groupIDs.length; i++) {
        UserGroup userGroup = (UserGroup) (userGroupDAO.getObjectByID(TextUtility.parseIntegerObj(groupIDs[i])));
        groupNameList.add(userGroup.getGroupName());
      }
      request.setAttribute("groupNameList",groupNameList);
    }

    //get role name by IDs
    String[] roleIDs = mtmCalendar2UserActorFrm.getShareToUserRoles();
    if (!Utility.isEmpty(roleIDs)) {
      List roleNameList = new ArrayList();
      for (int i = 0; i < roleIDs.length; i++) {
        UserRole userRole = (UserRole) (userRoleDAO.getObjectByID(TextUtility.parseIntegerObj(roleIDs[i])));
        roleNameList.add(userRole.getRoleName());
      }
      request.setAttribute("roleNameList",roleNameList);
    }

    return super.execute(mapping, form, request, response);
  }


  /**
   * Insert a record
   * @param mapping   ActionMapping
   * @param form      AbstractActionForm
   * @param request   HttpServletRequest
   * @param response  HttpServletResponse
   * @throws ApplicationException
   */
  public void insertRecord(ActionMapping mapping,
                           AbstractActionForm form,
                           HttpServletRequest request,
                           HttpServletResponse response) throws
      ApplicationException {
    Connection conn = this.getConnection(request);
    try {
      SessionContainer sessionCon = this.getSessionContainer(request);
      MtmCalendarUserUserActorDAObject mtmCalendarUserUserActorDAO =
          new MtmCalendarUserUserActorDAObject(sessionCon, conn);

      String[] shareToUserIDs = ( (MaintMtmCalendarUserUserActorForm) form).
          getShareToUserIDs();

      if (shareToUserIDs != null) {
        for (int i = 0; i < shareToUserIDs.length; i++) {
          if (shareToUserIDs[i] != null && !"".equals(shareToUserIDs[i])) {
            MtmCalendarUserUserActor userUserActor = new
                MtmCalendarUserUserActor();
            userUserActor.setUserRecordID(sessionCon.getUserRecordID());
            userUserActor.setActorType(MtmCalendarUserUserActor.ACTOR_TYPE_USER);
            userUserActor.setActorID(new Integer(shareToUserIDs[i]));
            mtmCalendarUserUserActorDAO.insertObject(userUserActor);
          }
        }
      }

      String[] shareToUserRoles = ( (MaintMtmCalendarUserUserActorForm) form).
          getShareToUserRoles();
      if (shareToUserRoles != null) {
        for (int i = 0; i < shareToUserRoles.length; i++) {

          if (shareToUserRoles[i] != null && !"".equals(shareToUserRoles[i])) {
            MtmCalendarUserUserActor userUserActor = new
                MtmCalendarUserUserActor();
            userUserActor.setUserRecordID(sessionCon.getUserRecordID());
            userUserActor.setActorType(MtmCalendarUserUserActor.ACTOR_TYPE_ROLE);
            userUserActor.setActorID(new Integer(shareToUserRoles[i]));
            mtmCalendarUserUserActorDAO.insertObject(userUserActor);
          }
        }
      }

      String[] shareToUserGroups = ( (MaintMtmCalendarUserUserActorForm) form).
          getShareToUserGroups();

      if (shareToUserGroups != null) {
        for (int i = 0; i < shareToUserGroups.length; i++) {

          if (shareToUserGroups[i] != null && !"".equals(shareToUserGroups[i])) {
            MtmCalendarUserUserActor userUserActor = new
                MtmCalendarUserUserActor();
            userUserActor.setUserRecordID(sessionCon.getUserRecordID());
            userUserActor.setActorType(MtmCalendarUserUserActor.
                                       ACTOR_TYPE_GROUP);
            userUserActor.setActorID(new Integer(shareToUserGroups[i]));
            mtmCalendarUserUserActorDAO.insertObject(userUserActor);
          }
        }
      }
      this.commit(request);

    } catch (ApplicationException appEx) {
      this.rollback(request);
      throw appEx;
    } catch (Exception e) {
      log.error(e);
      this.rollback(request);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    } finally {
    }
  }

  /**
   * Delete a record
   * @param mapping     ActionMapping
   * @param form        AbstractActionForm
   * @param request     HttpServletRequest
   * @param response    HttpServletResponse
   * @throws ApplicationException
   */
  public void deleteRecord(ActionMapping mapping,
                           AbstractActionForm form,
                           HttpServletRequest request,
                           HttpServletResponse response)
      throws ApplicationException {
  }

  /**
   * Select record
   * @param mapping    ActionMapping
   * @param form       AbstractActionForm
   * @param request    HttpServletRequest
   * @param response   HttpServletResponse
   * @throws ApplicationException
   */
  public void selectRecord(ActionMapping mapping,
                           AbstractActionForm form,
                           HttpServletRequest request,
                           HttpServletResponse response)
      throws ApplicationException {
    Connection conn = this.getConnection(request);
    List shareToUserList = new ArrayList();
    List shareToGroupList = new ArrayList();
    List shareToRoleList = new ArrayList();
    String[] shareToUserIDs = null;
    String[] shareToUserGroups = null;
    String[] shareToUserRoles = null;
    Integer ID = null;

    try {
      SessionContainer sessionCon = this.getSessionContainer(request);
      MtmCalendarUserUserActorDAObject mtmCalendarUserUserActorDAO = new
          MtmCalendarUserUserActorDAObject(sessionCon, conn);

      shareToUserList = mtmCalendarUserUserActorDAO.getListBySharerIDActorType(
          sessionCon.getUserRecordID(),
          new String[] {MtmCalendarUserUserActor.ACTOR_TYPE_USER});
      shareToUserIDs = new String[shareToUserList.size()];

      for (int i = 0; i < shareToUserList.size(); i++) {
        MtmCalendarUserUserActor tmpRecord = (MtmCalendarUserUserActor)
            shareToUserList.get(i);
        ID = tmpRecord.getActorID();
        shareToUserIDs[i] = tmpRecord.getActorID().toString();
      }
      ( (MaintMtmCalendarUserUserActorForm) form).setShareToUserIDs(
          shareToUserIDs);

      shareToRoleList = mtmCalendarUserUserActorDAO.getListBySharerIDActorType(
          sessionCon.getUserRecordID(),
          new String[] {MtmCalendarUserUserActor.ACTOR_TYPE_ROLE});
      shareToUserRoles = new String[shareToRoleList.size()];

      for (int i = 0; i < shareToRoleList.size(); i++) {
        MtmCalendarUserUserActor tmpRecord = (MtmCalendarUserUserActor)
            shareToRoleList.get(i);
        ID = tmpRecord.getActorID();
        shareToUserRoles[i] = tmpRecord.getActorID().toString();
      }
      ( (MaintMtmCalendarUserUserActorForm) form).setShareToUserRoles(
          shareToUserRoles);

      shareToGroupList = mtmCalendarUserUserActorDAO.getListBySharerIDActorType(
          sessionCon.getUserRecordID(),
          new String[] {MtmCalendarUserUserActor.ACTOR_TYPE_GROUP});
      shareToUserGroups = new String[shareToGroupList.size()];

      for (int i = 0; i < shareToGroupList.size(); i++) {
        MtmCalendarUserUserActor tmpRecord = (MtmCalendarUserUserActor)
            shareToGroupList.get(i);
        ID = tmpRecord.getActorID();
        shareToUserGroups[i] = tmpRecord.getActorID().toString();
      }
      form.setID(ID.toString());
      ( (MaintMtmCalendarUserUserActorForm) form).setShareToUserGroups(
          shareToUserGroups);

    } catch (ApplicationException appEx) {
      throw appEx;
    } catch (Exception e) {
      log.error(e);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    } finally {
    }
  }

  /**
   * Update a record
   *  @param mapping    ActionMapping
   * @param form       AbstractActionForm
   * @param request    HttpServletRequest
   * @param response   HttpServletResponse
   * @throws ApplicationException
   */
  public void updateRecord(ActionMapping mapping,
                           AbstractActionForm form,
                           HttpServletRequest request,
                           HttpServletResponse response)
      throws ApplicationException {
    Connection conn = this.getConnection(request);
    try {
      SessionContainer sessionCon = this.getSessionContainer(request);
      MtmCalendarUserUserActorDAObject mtmCalendarUserUserActorDAO = new MtmCalendarUserUserActorDAObject(sessionCon, conn);

      mtmCalendarUserUserActorDAO.deleteListBySharerIDActorType(sessionCon.getUserRecordID(), null);

      String[] shareToUserIDs = ((MaintMtmCalendarUserUserActorForm)form).getShareToUserIDs();

      if (shareToUserIDs != null) {
        for (int i = 0; i < shareToUserIDs.length; i++) {

          if (shareToUserIDs[i] != null && !"".equals(shareToUserIDs[i])) {
            MtmCalendarUserUserActor userUserActor = new MtmCalendarUserUserActor();
            userUserActor.setUserRecordID(sessionCon.getUserRecordID());
            userUserActor.setActorType(MtmCalendarUserUserActor.ACTOR_TYPE_USER);
            userUserActor.setActorID(new Integer(shareToUserIDs[i]));
            mtmCalendarUserUserActorDAO.insertObject(userUserActor);
          }
        }
      }

      String[] shareToUserRoles = ((MaintMtmCalendarUserUserActorForm)form).getShareToUserRoles();

      if (shareToUserRoles != null) {
        for (int i = 0; i < shareToUserRoles.length; i++) {

          if (shareToUserRoles[i] != null && !"".equals(shareToUserRoles[i])) {
            MtmCalendarUserUserActor userUserActor = new MtmCalendarUserUserActor();
            userUserActor.setUserRecordID(sessionCon.getUserRecordID());
            userUserActor.setActorType(MtmCalendarUserUserActor.ACTOR_TYPE_ROLE);
            userUserActor.setActorID(new Integer(shareToUserRoles[i]));
            mtmCalendarUserUserActorDAO.insertObject(userUserActor);
          }
        }
      }

      String[] shareToUserGroups = ( (MaintMtmCalendarUserUserActorForm) form).getShareToUserGroups();

      if (shareToUserGroups != null) {
        for (int i = 0; i < shareToUserGroups.length; i++) {

          if (shareToUserGroups[i] != null && !"".equals(shareToUserGroups[i])) {
            MtmCalendarUserUserActor userUserActor = new
                MtmCalendarUserUserActor();
            userUserActor.setUserRecordID(sessionCon.getUserRecordID());
            userUserActor.setActorType(MtmCalendarUserUserActor.
                                       ACTOR_TYPE_GROUP);
            userUserActor.setActorID(new Integer(shareToUserGroups[i]));
            mtmCalendarUserUserActorDAO.insertObject(userUserActor);
          }
        }
      }
      this.commit(request);
    } catch (ApplicationException appEx) {
      this.rollback(request);
      throw appEx;
    } catch (Exception e) {
      log.error(e);
      this.rollback(request);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    } finally {
    }
  }

}


